#include<bits/stdc++.h> 
#define int long long
#define endl "\n"
#define fread(x) ios::sync_with_stdio(x)
using namespace std;
class Union{
public:
	vector<int> parent;
	vector<int> rank;
	Union(int num){
		parent.resize(num);
		rank.resize(num,1);
		for(int i = 0; i < num ;i++){
			parent[i] = i;
		}
	}
	int find(int x){
		if(parent[x] != x){
			parent[x] = find(parent[x]);
		}
		return parent[x];
	}
	void unite(int x ,int y){
		int rx = find(x);
		int ry = find(y);
		if(rank[rx] < rank[ry]){
			parent[rx] = ry;
		}else if(rank[rx] > rank[ry] )
		{
			parent[ry] = rx;
		}
		else {
			parent[ry] = rx;
			rank[rx]++;
		}
		
	}
};
signed main(){
	fread(0);
	int n,m;
	while(cin >> n >>m){
		if(n == 0) return 0;
		if(m == 0){
			cout << n - 1<<endl;
			return 0;
		}
		
		Union u(n+1);
		bool flag[n+1];
		memset(flag,false,sizeof flag);
		for(int i = 0 ; i < m ;i ++){
			int x,y;
			cin >> x >> y;
			u.unite(x,y);
		}
		int sum = 0;
		for(int i=1;i<=n;i++)
		{
			if(u.find(i)==i)
			{
				sum++;
			}
		}
		cout<<sum-1<<endl;
	}
	
	return 0;
}
